﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.Entity;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using SosBL;
using SosData;


namespace SosForms
{
    public partial class FormDetalle : FormBase
    {

        #region " Constructores "

        /// <summary>
        /// Constructor
        /// </summary>
        public FormDetalle()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="entidad">entidad</param>
        public FormDetalle(object entidad)
        {
            InitializeComponent();
            bindingSource.DataSource = entidad;
        }

        #endregion

        #region " Variables "


        public bool cancelado = true;
        public EntityState state = EntityState.Unchanged;
        public object entidad = null;
        private object entidadOriginal = null;

        public bool puedeAñadir { get; set; }
        public bool puedeEditar { get; set; }
        public bool puedeBorrar { get; set; }
        public bool puedeNavegar { get; set; }


        public enum _modoForm
        {
            Añadir,
            Editar,
            Navegacion
        }

        public _modoForm ModoForm { get; set; }
        #endregion

        #region " Metodos datos "
        /// <summary>
        /// Añadir un nuevo elemento
        /// </summary>
        public virtual void Nuevo()
        {
            EstadoControlesForm(EstadoControles.Habilitar);
            tsButNuevo.Enabled = false;
            tsButModificar.Enabled = false;
            tsButEliminar.Enabled = false;
            tsButGuardar.Enabled = true;
            tsButCancelar.Enabled = true;

            state = EntityState.Added;
            entidadOriginal = entidad;
            bindingSource.DataSource = entidad;
            bindingSource.AddNew();
        }

        /// <summary>
        /// Editar elemento
        /// </summary>
        public virtual void Modificar()
        {
            estadoForm(_modoForm.Editar);

            entidadOriginal = entidad;
            state = EntityState.Modified;

        }

        /// <summary>
        /// Eliminar elemento
        /// </summary>
        public virtual void Eliminar()
        {
            if (MessageBox.Show("¿Seguro que quiere eliminar este elemento?", "Eliminar", MessageBoxButtons.YesNo, MessageBoxIcon.Warning) == System.Windows.Forms.DialogResult.Yes)
            {
                using (var db = new sosContext())
                {
                    entidad = bindingSource.Current;
                    db.Entry(entidad).State = EntityState.Deleted;
                    db.SaveChanges();
                }
              //  SosBL.DataBL.GuardarCambios(entidad, EntityState.Deleted);
            }
        }

        /// <summary>
        /// Cancelar cambios
        /// </summary>
        public virtual void Cancelar()
        {
            bindingSource.CancelEdit();
            if (state == EntityState.Modified)
            {
                try
                {
                    bindingSource.DataSource = entidadOriginal;
                }
                catch { }
            }
            if (state == EntityState.Added)
            {
                try
                {
                    bindingSource.DataSource = entidadOriginal;
                }
                catch { }
            }

            bindingSource.ResetBindings(false);

            tsButNuevo.Enabled = true;
            tsButModificar.Enabled = true;
            tsButEliminar.Enabled = true;
            tsButGuardar.Enabled = false;
            tsButCancelar.Enabled = false;

            state = EntityState.Unchanged;

            cancelado = true;
            estadoForm(_modoForm.Navegacion);
        }

        /// <summary>
        /// Guardar cambios realizados
        /// </summary>
        public virtual void Guardar()
        {
            cancelado = false;
            using (var db = new sosContext())
            {
                MessageBox.Show(state.ToString());
                db.Entry(entidad).State = state;
                db.SaveChanges();
            }
            //SosBL.DataBL.GuardarCambios(entidad, state);
            estadoForm(_modoForm.Navegacion);
        }

        public void estadoForm(_modoForm modo)
        {
            switch (modo)
            {
                case _modoForm.Añadir:
                    EstadoControlesForm(EstadoControles.Habilitar);
                    tsButNuevo.Enabled = false;
                    tsButModificar.Enabled = false;
                    tsButEliminar.Enabled = false;
                    tsButGuardar.Enabled = true;
                    tsButCancelar.Enabled = true;

                    state = EntityState.Added;
                    break;

                case _modoForm.Editar:
                    EstadoControlesForm(EstadoControles.Habilitar);
                    tsButNuevo.Enabled = false;
                    tsButModificar.Enabled = false;
                    tsButEliminar.Enabled = false;
                    tsButGuardar.Enabled = true;
                    tsButCancelar.Enabled = true;

                    state = EntityState.Modified;
                    break;

                case _modoForm.Navegacion:
                    EstadoControlesForm(EstadoControles.Deshabilitar);
                    tsButNuevo.Enabled = true;
                    tsButModificar.Enabled = true;
                    tsButEliminar.Enabled = true;
                    tsButGuardar.Enabled = false;
                    tsButCancelar.Enabled = false;

                    state = EntityState.Unchanged;

                    tsButNuevo.Enabled = puedeAñadir;
                    tsButModificar.Enabled = puedeEditar;
                    tsButEliminar.Enabled = puedeBorrar;

                    break;

                default:

                    break;
            }
        }

        /// <summary>
        ///  cancel data
        /// </summary>
        public virtual void CerrarForm()
        {
            bindingSource.CancelEdit();
        }

        #endregion

        #region " Controles "

        /// <summary>
        /// Añadir nuevo elemento
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void tsButNuevo_Click(object sender, EventArgs e)
        {
            this.Nuevo();
        }



        /// <summary>
        /// Modificar nuevo elemento
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tsButModificar_Click(object sender, EventArgs e)
        {
            this.Modificar();
        }

        /// <summary>
        /// Eliminar nuevo elemento
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void tsButEliminar_Click(object sender, EventArgs e)
        {
            this.Eliminar();
        }

        /// <summary>
        /// Cancelar operacion
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void tsButCancelar_Click(object sender, EventArgs e)
        {
            this.Cancelar();
        }

        /// <summary>
        /// Guardar cambios
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void tsButGuardar_Click(object sender, EventArgs e)
        {
            this.Guardar();
        }

        /// <summary>
        /// Anterior
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void tsButAnterior_Click(object sender, EventArgs e)
        {
            this.bindingSource.MovePrevious();
        }

        /// <summary>
        /// Siguiente
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void tsButSiguiente_Click(object sender, EventArgs e)
        {
            this.bindingSource.MoveNext();
        }
        #endregion

        #region " Eventos Form "

        /// <summary>
        /// Form load
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void FormDetalle_Load(object sender, EventArgs e)
        {
            if (!DesignMode)
            {
                estadoForm(ModoForm);
            }
        }
        /// <summary>
        /// Form closing
        /// </summary>
        /// <param name="sender">sender</param>
        /// <param name="e">e</param>
        private void FormDetalle_FormClosing(object sender, FormClosingEventArgs e)
        {
            CerrarForm();
        }

        #endregion

    }
}
